/**
 * 
 */
package soa.security.domain;

import com.fasterxml.jackson.annotation.JsonIgnore;
import org.springframework.ldap.odm.annotations.*;
import org.springframework.ldap.support.LdapNameBuilder;
import soa.security.Constants;

import javax.naming.Name;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlTransient;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

/**
 * @author CreGu
 *
 */
@XmlRootElement
@XmlAccessorType(XmlAccessType.FIELD)
@Entry(objectClasses = { "zjtzsw-organization", "organization", "zjtzsw-bid" })
public final class Organization extends SoaBid {

	private static final long serialVersionUID = 4199359620577225542L;

	@XmlTransient
	@JsonIgnore
	@Id
	private Name dn;

	@DnAttribute(value = "bid")
	private String bid;

	@Attribute(name = "o")
	private String name;

	@Attribute(name = "ccode")
	private String code;

	private String organizationCode;

	private String regionCode;

	private String shortName;

	private String ename;

	private String organizationType;

	private String telephoneNumber;

	private String principalName;

	@Attribute(name = "IDType")
	private String principalIDType;

	@Attribute(name = "IDNumber")
	private String principalIDNumber;

	@Attribute(name = "departmentAddress")
	private String address;

	private String description;

	private int priority;

	private String createTime;

	private String parentBid;

	/**
	 * 
	 */
	@XmlTransient
	@JsonIgnore
	@Transient
	private Organization parent;
	@XmlTransient
	@JsonIgnore
	@Transient
	private Region region;
	@XmlTransient
	@JsonIgnore
	@Transient
	private String regionDn;

	public static Organization create(String uid, String name,
			String organizationCode) {
		return new Organization(uid, name, organizationCode);
	}

	public Organization() {
	}

	public Organization(String bid, String name, String organizationCode) {
		super();
		this.bid = bid;
		this.name = name;
		this.organizationCode = organizationCode;
	}

	public Organization(String bid, String name, String ename,
			String organizationCode) {
		super();
		this.bid = bid;
		this.name = name;
		this.ename = ename;
		this.organizationCode = organizationCode;
	}

	public Name buildDn(String regionDn) {
		LdapNameBuilder builder = LdapNameBuilder.newInstance(regionDn);
		if (getParent() != null)
			builder.add(getParent().getDn());

		builder.add(Constants.BID, getBid());
		return builder.build();
	}

	public Name getDn() {
		return dn;
	}

	public void setDn(Name dn) {
		this.dn = dn;
	}

	public String getBid() {
		return bid;
	}

	public void setBid(String uid) {
		this.bid = uid;
	}

	public Organization getParent() {
		return parent;
	}

	public void setParent(Organization parent) {
		this.parent = parent;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getEname() {
		return ename;
	}

	public void setEname(String ename) {
		this.ename = ename;
	}

	public String getOrganizationCode() {
		return organizationCode;
	}

	public void setOrganizationCode(String organizationCode) {
		this.organizationCode = organizationCode;
	}

	public String getPrincipalName() {
		return principalName;
	}

	public void setPrincipalName(String principalName) {
		this.principalName = principalName;
	}

	public String getPrincipalIDType() {
		return principalIDType;
	}

	public void setPrincipalIDType(String principalIDType) {
		this.principalIDType = principalIDType;
	}

	public String getPrincipalIDNumber() {
		return principalIDNumber;
	}

	public void setPrincipalIDNumber(String principalIDNumber) {
		this.principalIDNumber = principalIDNumber;
	}

	public String getOrganizationType() {
		return organizationType;
	}

	public void setOrganizationType(String organizationType) {
		this.organizationType = organizationType;
	}

	public String getDescription() {
		return description;
	}

	public void setDescription(String description) {
		this.description = description;
	}

	public int getPriority() {
		return priority;
	}

	public void setPriority(int priority) {
		this.priority = priority;
	}

	public String getCreateTime() {
		return createTime;
	}

	public void setCreateTime(String createTime) {
		this.createTime = createTime;
	}

	public String getRegionDn() {
		return regionDn;
	}

	public void setRegionDn(String regionDn) {
		this.regionDn = regionDn;
	}

	public String getCode() {
		return code;
	}

	public void setCode(String code) {
		this.code = code;
	}

	public String getShortName() {
		return shortName;
	}

	public void setShortName(String shortName) {
		this.shortName = shortName;
	}

	public String getTelephoneNumber() {
		return telephoneNumber;
	}

	public void setTelephoneNumber(String telephoneNumber) {
		this.telephoneNumber = telephoneNumber;
	}

	public String getAddress() {
		return address;
	}

	public void setAddress(String address) {
		this.address = address;
	}

	public Region getRegion() {
		return region;
	}

	public void setRegion(Region region) {
		this.region = region;
	}

	public String getRegionCode() {
		return regionCode;
	}

	public void setRegionCode(String regionCode) {
		this.regionCode = regionCode;
	}

	public String getParentBid() {
		return parentBid;
	}

	public void setParentBid(String parentBid) {
		this.parentBid = parentBid;
	}

	@XmlRootElement
	@XmlAccessorType(XmlAccessType.FIELD)
	public static class Organizations {
		protected List<Organization> organization = new ArrayList<Organization>();

		public List<Organization> getOrganization() {
			return this.organization;
		}

		public void addOrganization(Organization organization) {
			this.organization.add(organization);
		}

		public boolean addAll(Collection<? extends Organization> c) {
			return organization.addAll(c);
		}
	}

}
